package BandB.Tool.GPhoneManager.FileExplorer;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

//import android.util.Log;

public class ZipHelper {

	// private static final String TAG = ZipHelper.class.getSimpleName();

	public static void zip(List<String> filePathes, String toFilePath,
			boolean isDelete) {
		zipFiles(filePathes, toFilePath);
	}

	private static ArrayList<File> getFiles(String filePath) {
		ArrayList<File> fileList = new ArrayList<File>();
		File apkFolder = new File(filePath);
		if (apkFolder.isFile()) {
			fileList.add(apkFolder);
			return fileList;
		}
		if (!apkFolder.exists()) {
			apkFolder.mkdirs();
		}
		File[] files = apkFolder.listFiles();
		if (files == null) {
			return fileList;
		}
		int length = files.length;
		for (int i = 0; i < length; i++) {
			String path = files[i].getAbsolutePath();
			// Log.i(TAG + "path is :", path);
			if (files[i].isFile()) {
				fileList.add(files[i]);
			} else {
				if (files[i].isDirectory()) {
					List<File> temp = getFiles(path);
					for (File file : temp) {
						if (file.isFile()) {
							fileList.add(file);
						}
					}
				}
			}
		}
		return fileList;
	}

	private static ArrayList<File> getFilesForDirectories(List<String> path) {
		ArrayList<File> list = new ArrayList<File>();
		for (String string : path) {
			ArrayList<File> list2 = getFiles(string);
			for (File file : list2) {
				list.add(file);
			}
		}

		return list;
	}

	private static void zipFiles(List<String> pathes, String zipname) {
		FileOutputStream fos = null;
		ZipOutputStream zipOut = null;
		ArrayList<File> files = getFilesForDirectories(pathes);
		try {
			fos = new FileOutputStream(zipname);
			zipOut = new ZipOutputStream(fos);

			int length = files.size();
			for (int i = 0; i < length; i++) {
				File f = files.get(i);
				FileInputStream fis = new FileInputStream(files.get(i));
				ZipEntry ze = new ZipEntry(f.getName());
				zipOut.putNextEntry(ze);
				int c = 0;
				while ((c = fis.read()) != -1) {
					zipOut.write(c);
				}
				fis.close();
			}
			zipOut.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
			// Log.e(TAG, "files zip error.");
		} catch (IOException ex) {
			ex.printStackTrace();
			// Log.e(TAG, "files zip error");
		}
	}

	//
	// private static void zipFile(String filePath, String zipname) {
	// FileOutputStream fos = null;
	// ZipOutputStream zipOut = null;
	//
	// try {
	// fos = new FileOutputStream(zipname);
	// zipOut = new ZipOutputStream(fos);
	//
	// File f = new File(filePath);
	// FileInputStream fis = new FileInputStream(filePath);
	// ZipEntry ze = new ZipEntry(f.getName());
	// zipOut.putNextEntry(ze);
	// int c = 0;
	// while ((c = fis.read()) != -1) {
	// zipOut.write(c);
	// }
	// fis.close();
	//
	// zipOut.close();
	// } catch (FileNotFoundException ex) {
	// ex.printStackTrace();
	// // Log.e(TAG, "zip file error.");
	// } catch (IOException ex) {
	// ex.printStackTrace();
	// // Log.e(TAG, "zip file error.");
	// }
	//
	// }

	public static void zipExtracting(String pathName, String fileName) {
		try {
			int BUFFER = 2048;
			ZipFile zipFile = new ZipFile(fileName);
			String name = zipFile.getName();
			String zipName = name.substring(0, name.lastIndexOf("."));
			// System.out.println(zipName + "count:" + zipFile.entries);
			Enumeration<?> enu = zipFile.entries();
			// int i = 0;

			while (enu.hasMoreElements()) {
				ZipEntry zipEntry = (ZipEntry) enu.nextElement();

				if (zipEntry.isDirectory()) {
					new File(zipName + File.separator + zipEntry.getName())
							.mkdirs();
					// System.out.println(zipName + File.separator
					// + zipEntry.getName());
					continue;
				}
				BufferedInputStream bis = new BufferedInputStream(zipFile
						.getInputStream(zipEntry));

				// String parentPath = new File(fileName).getParent();
				File file = new File(zipName + File.separator
						+ zipEntry.getName());
				// System.out.println(zipName + File.separator
				// + zipEntry.getName());
				int lastIndex = zipEntry.getName().lastIndexOf("/");
				if (lastIndex != -1) {
					String folderPath = zipEntry.getName().substring(0,
							lastIndex);
					File folder = new File(zipName + File.separator
							+ folderPath);
					folder.mkdirs();
				}
				File temp = new File(zipName);
				if (!temp.exists()) {
					temp.mkdirs();
				}

				file.createNewFile();

				FileOutputStream fos = new FileOutputStream(file);
				BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

				// int count;
				byte[] array = new byte[BUFFER];
				while ((bis.read(array, 0, BUFFER)) != -1) {
					bos.write(array, 0, BUFFER);
				}

				bos.flush();
				bos.close();
				bis.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
